﻿using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;

using Game.Map.Extensions;
using Game.Surface;
using Game.Map;

namespace Game.Editor
{
    public class EditorSurfaceField : SurfaceField
    {
        public EditorSurfaceField(
            IMapFieldView field,
            AbstractField top,
            AbstractField cloud)
            : base(field, top, cloud)
        {
            field.VehicleX().VehicleAssigned += (source, args) => OnVehicleChanged();
            field.ObjectX().ObjectAssigned += (source, args) => OnVehicleChanged();
            field.Changed += (source, args) => OnVehicleChanged();

            field.IsVisible = true;
            OnVehicleChanged();
        }

        protected override void UpdateBitmap(IMapFieldView field)
        {
            Fill = CreateBrush(field);
        }

        public static Brush CreateBrush(IMapFieldView field)
        {
            DrawingVisual drawingVisual = new DrawingVisual();
            DrawingContext context = drawingVisual.RenderOpen();

//            SurfaceFieldRenderer.RenderTerrain(field, context);
//            SurfaceFieldRenderer.RenderMapObject(field, context);
//            SurfaceFieldRenderer.RenderVehicle(field.VehicleX().Vehicle, context);

            context.Close();
            return new VisualBrush { Visual = drawingVisual };
        }

        void OnVehicleChanged()
        {

            var menu = new ContextMenu
                           {
                               VerticalAlignment = VerticalAlignment.Top, 
                               HorizontalAlignment = HorizontalAlignment.Left
                           };

            var itemClear = new MenuItem {Header = "Clear"};
            menu.Items.Add(itemClear);
            itemClear.Click += (source, args) =>
                                   {
                                       MapField.VehicleX().Vehicle = null;
                                       MapField.ObjectX().GameObject = null;
                                   };

            var itemHuman = new MenuItem {Header = "Human"};
            menu.Items.Add(itemHuman);
            itemHuman.Click += delegate
            {
                if (MapField.VehicleX().Vehicle != null)
                {
                    MapField.VehicleX().Vehicle.Owner = Common.HumanPlayer.Player;
                }
                else
                {
                    var obj = MapField.ObjectX().GameObject;
                    Debug.Assert(obj != null, "obj != null");
                    obj.Building.Owner = Common.HumanPlayer.Player;
                }
            };

            var itemAi = new MenuItem {Header = "AI"};
            menu.Items.Add(itemAi);
            itemAi.Click += delegate
            {
                if (MapField.VehicleX().Vehicle != null)
                {
                    MapField.VehicleX().Vehicle.Owner = Common.AiPlayer.Player;
                }
                else
                {
                    var obj = MapField.ObjectX().GameObject;
                    Debug.Assert(obj != null, "obj != null");
                    obj.Building.Owner = Common.AiPlayer.Player;
                }
            };

            if (MapField.VehicleX().Vehicle != null)
            {
                itemHuman.IsEnabled = true;
                itemAi.IsEnabled = true;
            }
            else if (MapField.ObjectX().GameObject != null)
            {
                itemHuman.IsEnabled = true;
                itemAi.IsEnabled = true;
            }
            else
            {
                itemHuman.IsEnabled = false;
                itemAi.IsEnabled = false;
            }

            ContextMenuService.SetContextMenu(SurfaceCloudField, menu);
        }

    }
}
